//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "TransformedGrid.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/Array.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CTransformedGrid::CTransformedGrid (Cephei::QL::Math::IArray^ grid) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CArray^ _Cgrid;
    try
    {
#ifdef HANDLE
        _phTransformedGrid = NULL;
#endif
        _Cgrid = safe_cast<CArray^> (grid);
        _Cgrid->Lock();
        QuantLib::Array& _grid = static_cast<QuantLib::Array&> (_Cgrid->GetReference ()); 
        _ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (new QuantLib::TransformedGrid ( _grid ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrid != nullptr) _Cgrid->Unlock();
    }
}
Cephei::QL::Math::CTransformedGrid::CTransformedGrid (boost::shared_ptr<QuantLib::TransformedGrid>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTransformedGrid = NULL;
#endif
	_ppTransformedGrid = &childNative;
    
}
Cephei::QL::Math::CTransformedGrid::CTransformedGrid (QuantLib::TransformedGrid& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTransformedGrid = NULL;
#endif
	_ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (&childNative);
    
    _TransformedGridOwner = owner;
    
}

Cephei::QL::Math::CTransformedGrid::CTransformedGrid (CTransformedGrid^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTransformedGrid = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CTransformedGrid::CTransformedGrid (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTransformedGrid = NULL;
#endif
	if (!t->IsSubclassOf(CTransformedGrid::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CTransformedGrid::CTransformedGrid (QuantLib::Handle<QuantLib::TransformedGrid>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTransformedGrid = &childNative;
	_ppTransformedGrid = &static_cast<boost::shared_ptr<QuantLib::TransformedGrid>>(childNative.currentLink());
    
    _TransformedGridOwner = owner;
}
Cephei::QL::Math::CTransformedGrid::CTransformedGrid (QuantLib::Handle<QuantLib::TransformedGrid> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTransformedGrid = &childNative;
	_ppTransformedGrid = &static_cast<boost::shared_ptr<QuantLib::TransformedGrid>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CTransformedGrid::CTransformedGrid (QuantLib::TransformedGrid childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTransformedGrid = NULL;
#endif
	_ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (new QuantLib::TransformedGrid (childNative));
    
}
#endif

Cephei::QL::Math::CTransformedGrid::~CTransformedGrid ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTransformedGrid != NULL)
    {
	    delete _ppTransformedGrid;
        _ppTransformedGrid = NULL;
    }
}
Cephei::QL::Math::CTransformedGrid::!CTransformedGrid ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTransformedGrid != NULL)
    {
	    delete _ppTransformedGrid;
    }
}
QuantLib::TransformedGrid& Cephei::QL::Math::CTransformedGrid::GetReference ()
{
    if (_ppTransformedGrid == NULL) throw gcnew NativeNullException ();
	return **_ppTransformedGrid;
}
boost::shared_ptr<QuantLib::TransformedGrid>& Cephei::QL::Math::CTransformedGrid::GetShared ()
{
    if (_ppTransformedGrid == NULL) throw gcnew NativeNullException ();
	return *_ppTransformedGrid;
}
QuantLib::TransformedGrid* Cephei::QL::Math::CTransformedGrid::GetPointer ()
{
    if (_ppTransformedGrid == NULL) throw gcnew NativeNullException ();
	return &**_ppTransformedGrid;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TransformedGrid>& Cephei::QL::Math::CTransformedGrid::GetHandle ()
{
	if (_phTransformedGrid == NULL)
	{
		_phTransformedGrid = new Handle<QuantLib::TransformedGrid> (*_ppTransformedGrid);
	}
	return *_phTransformedGrid;
}
#endif
bool Cephei::QL::Math::CTransformedGrid::HasNative () 
{
	return (_ppTransformedGrid != NULL);
}

Double Cephei::QL::Math::CTransformedGrid::Dx (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTransformedGrid)->dx ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CTransformedGrid::DxArray::get ()
{
    try
    {
    	QuantLib::Array & _rv = (QuantLib::Array &)(*_ppTransformedGrid)->dxArray ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CTransformedGrid::Dxm (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTransformedGrid)->dxm ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CTransformedGrid::DxmArray::get ()
{
    try
    {
    	QuantLib::Array & _rv = (QuantLib::Array &)(*_ppTransformedGrid)->dxmArray ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CTransformedGrid::Dxp (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTransformedGrid)->dxp ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CTransformedGrid::DxpArray::get ()
{
    try
    {
    	QuantLib::Array & _rv = (QuantLib::Array &)(*_ppTransformedGrid)->dxpArray ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CTransformedGrid::Grid (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTransformedGrid)->grid ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CTransformedGrid::GridArray::get ()
{
    try
    {
    	QuantLib::Array & _rv = (QuantLib::Array &)(*_ppTransformedGrid)->gridArray ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::CTransformedGrid::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTransformedGrid)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CTransformedGrid::TransformedGrid (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTransformedGrid)->transformedGrid ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CTransformedGrid::TransformedGridArray::get ()
{
    try
    {
    	QuantLib::Array & _rv = (QuantLib::Array &)(*_ppTransformedGrid)->transformedGridArray ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::ITransformedGrid^ Cephei::QL::Math::CTransformedGrid_Factory::Create (Cephei::QL::Math::IArray^ grid)
{
    return gcnew CTransformedGrid ( grid);
}
